<template>
  <div :class="className" :style="{ height: height, width: width }" />
</template>

<script>
import resize from '@/mixins/resize'

export default {
  mixins: [resize],
  props: {
    className: {
      type: String,
      default: 'funnel'
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      type: String,
      default: '200px'
    }
  },
  data() {
    return {
      chart: null
    }
  },
  mounted() {
    this.$nextTick(() => {
      this.initChart()
    })
  },
  beforeDestroy() {
    if (!this.chart) {
      return
    }
    this.chart.dispose()
    this.chart = null
  },
  methods: {
    initChart() {
      this.chart = this.$echarts.init(this.$el)
      this.chart.setOption({
        title: {
          text: '各\n国\n双\n碳\n行\n动\n贡\n献\n度',
          textStyle: {
            fontSize: 12,
            color: '#fff'
          },
          x: '5%',
          y: 'center'
        },
        legend: {
          show: false
        },
        grid: {
          left: '15%'
        },
        series: [
          {
            type: 'funnel',
            top: 4,
            left: '15%',
            bottom: 8,
            width: '70%',
            gap: 2,
            minSize: '0%',
            maxSize: '100%',
            label: {
              normal: {
                position: 'right',
                formatter: function (params) {
                  return params.name
                }
              }
            },
            labelLine: {
              normal: {
                length: 20,
                position: 'left',
                lineStyle: {
                  width: 0
                }
              }
            },
            itemStyle: {
              normal: {
                borderWidth: 0
              }
            },
            data: [
              {
                value: 80,
                name: '中国',
                itemStyle: {
                  normal: {
                    color: '#54bcff'
                  }
                }
              },
              {
                value: 75,
                name: '欧盟',
                itemStyle: {
                  normal: {
                    color: '#0695d5'
                  }
                }
              },
              {
                value: 66,
                name: '美国',
                itemStyle: {
                  normal: {
                    color: '#0070ad'
                  }
                }
              },
              {
                value: 40,
                name: '日本',
                itemStyle: {
                  normal: {
                    color: '#004d86'
                  }
                }
              },
              {
                value: 33,
                name: '韩国',
                itemStyle: {
                  normal: {
                    color: '#002d61'
                  }
                }
              }
            ]
          },
          {
            name: '',
            type: 'funnel',
            top: 4,
            left: '15%',
            bottom: 8,
            width: '70%',
            gap: 2,
            label: {
              normal: {
                position: 'inside',
                formatter: '{c}%',
                textStyle: {
                  color: '#fff'
                }
              }
            },
            labelLine: {
              normal: {}
            },
            itemStyle: {
              normal: {
                color: 'transparent',
                borderWidth: 0,
                opacity: 0
              }
            },
            data: [
              { value: 80, name: '中国' },
              { value: 75, name: '欧盟' },
              { value: 66, name: '美国' },
              { value: 40, name: '日本' },
              { value: 33, name: '韩国' }
            ]
          },
          {
            top: 0,

            name: '',
            type: 'funnel',
            left: '15%',
            bottom: 1,
            width: '72%',
            minSize: '0%',
            maxSize: '100%',
            label: {
              show: false
            },
            labelLine: {
              show: false
            }
            // itemStyle: {
            //   normal: {
            //     color: 'transparent',
            //     borderWidth: 1,
            //     borderColor: '#ddd'
            //   }
            // },
            // data: [{ value: 100, name: 'border' }]
          }
        ]
      })
    }
  }
}
</script>
